﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;



namespace MainApp
{
    public partial class meter : UserControl
    {
        public float min = 0, max = 10, value = 0, startangle = (float)(-0.25), stopangle=(float)(1.25);
        //najmniejsza wyświetlana wartość, największa wyświetlana wartość, obecna wartość, kąt odpowiadający wartości najmniejszej, kąt odpowiadający wartości największej
        public int pointersize=2,displayscount=3,PointerPercentR=80;
        //rozmiar wskazówki,  ilość wyświetlaczy wartości,procentowa długość wskazówki;
        public System.Drawing.Color pointercolor = System.Drawing.Color.Black, textcolor = System.Drawing.Color.Black;
        //kolor wskazówki, kolor tekstu
        private int Xr, Yr;
        //rozmiary wskazówki dla danej osi
        string floatingCENTRE="F3", floatingREST="F2";

        private bool zainicjowano =false;
        private Label[] displays;
        public meter()
        {
            InitializeComponent();
        }

        private void meter_Load(object sender, EventArgs e)
        {
            min =0;
            max = 100;
            value =0;
            startangle = (float)(-0.25);
            stopangle = (float)(1.25);
            displays = new Label[displayscount];

            wskazowka.BorderWidth = pointersize;
            wskazowka.BorderColor = pointercolor;

            label1.Text = this.Name;
            label1.Font = this.Font;
            label1.ForeColor = this.ForeColor;
            label1.BackColor = this.BackColor;
            label1.Location = new System.Drawing.Point(this.Size.Width / 2 - label1.Size.Width / 2, this.Size.Height / 2 + label1.Size.Height);


            label2.Text = Convert.ToString(value);
            label2.Font = this.Font;
            label2.ForeColor = this.ForeColor;
            label2.BackColor = this.BackColor;
            label2.Location = new System.Drawing.Point(this.Size.Width / 2 - label2.Size.Width / 2, label1.Location.Y + label1.Size.Height);

            sizing();
            refresh();
            zainicjowano = true;
        }

        public void refresh()
        {
            float curangle;
            float tmpmax, tmpstopangle, tmpcurangle, tmpvalue;
            tmpmax = max - min;
            tmpstopangle = stopangle - startangle;
            tmpvalue = value - min;
            tmpcurangle = (tmpvalue * tmpstopangle) / tmpmax;
            curangle = tmpcurangle + startangle;
            wskazowka.Y2 = wskazowka.Y1 - (int)((PointerPercentR*wskazowka.Y1/100) * (Math.Sin(curangle * Math.PI)));
            wskazowka.X2 = wskazowka.X1 - (int)((PointerPercentR*wskazowka.X1/100) * (Math.Cos(curangle * Math.PI)));

            label2.Text = value.ToString(floatingCENTRE);
            label2.Location = new System.Drawing.Point(this.Size.Width / 2 - label2.Size.Width / 2, label1.Location.Y + label1.Size.Height);
            label1.Refresh();
            label2.Refresh();
        }

        private void sizing()
        {
            Yr = wskazowka.Y1 = this.Size.Height / 2;
            Xr = wskazowka.X1 = this.Size.Width / 2;
            int labX, labY;
            
            if(displayscount>0)
            {
                int i = 0;
                if (zainicjowano == false)
                {
                    displays[i] = new Label();
                    this.displays[i].AutoSize = true;
                    this.displays[i].Name = "display" + i;
                    this.displays[i].Size = new System.Drawing.Size(10, 10);
                    this.displays[i].TabIndex = 1;
                    this.displays[i].Font = this.Font;
                    this.displays[i].ForeColor = this.ForeColor;
                    displays[i].Location = new System.Drawing.Point(0, 0);
                    this.Controls.Add(displays[i]);
                    displays[i].Text = ((i * (max - min) / (displayscount - 1)) + min).ToString(floatingREST);
                }
                labX = (int)(wskazowka.X1 - (wskazowka.X1 * (Math.Cos(((stopangle - startangle) * ((float)(i) / (float)displayscount) + startangle) * Math.PI))));
                labY = (int)(wskazowka.Y1 - (wskazowka.Y1 * (Math.Sin(((stopangle - startangle) * ((float)(i) / (float)displayscount) + startangle) * Math.PI))));
                labX = labX - displays[i].Size.Width / 2;
                labY = labY - displays[i].Size.Height / 2;

                if (labX <= 0) labX = 0;
                if (labX >= this.Size.Width) labX = this.Size.Width - displays[i].Size.Width;
                if (labY <= 0) labY = 0;
                if (labY >= this.Size.Height) labY = this.Size.Height - displays[i].Size.Height;
                displays[i].Location = new System.Drawing.Point(labX, labY);
                displays[i].Update();

                for(i =1;i<displayscount;++i)
                {
                    if (zainicjowano == false)
                    {

                        displays[i] = new Label();
                        this.displays[i].AutoSize = true;
                        this.displays[i].Name = "display" + i;
                        this.displays[i].Size = new System.Drawing.Size(10, 10);
                        this.displays[i].TabIndex = 1;
                        this.displays[i].Font = this.Font;
                        this.displays[i].ForeColor = this.ForeColor;
                        displays[i].Text = ((i * (max - min) / (displayscount - 1)) + min).ToString(floatingREST);
                        displays[i].Location = new System.Drawing.Point(0, 0);
                        this.Controls.Add(displays[i]);
                    }
                
                    labX = (int)(wskazowka.X1 - (wskazowka.X1 * (Math.Cos(((stopangle - startangle) * ((float)(i) / (float)(displayscount-1)) + startangle) * Math.PI))));
                    
                    labY = (int)(wskazowka.Y1 - (wskazowka.Y1 * (Math.Sin(((stopangle - startangle) * ((float)(i) / (float)(displayscount-1)) + startangle) * Math.PI))) );
                    labX = labX - displays[i].Size.Width / 2;
                    labY = labY - displays[i].Size.Height / 2;
                    
                    if (labX <= 0) labX = 0;
                    if (labX >= this.Size.Width) labX = this.Size.Width - displays[i].Size.Width;
                    if (labY <= 0) labY = 0;
                    if (labY >= this.Size.Height) labY = this.Size.Height - displays[i].Size.Height;
                    displays[i].Location = new System.Drawing.Point(labX,labY);
                    displays[i].Update();
                }

            }

        }

        private void meter_Resize(object sender, EventArgs e)
        {
            if (zainicjowano == true)
            {
                sizing();
                refresh();
            }
        }
    }
}
